<script src="https://unpkg.com/react@17/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <div id="like_button_container"> </div> <!-- Load our React component. --> <script src="like_button.js"></script> <!-- like_button.js 'use strict'; const e = React.createElement; class LikeButton extends React.Component { constructor(props) { // props: object for attribute properties super(props); this.state = { liked: false }; } render() { if (this.state.liked) { return 'You liked this.'; } // JS return e( 'button', { onClick: () => this.setState({ liked: true }) }, 'Like' ); /* or // JSX // What do you think is returned? return ( <button onClick={() => this.setState({ liked: true })}> Like </button> // Is it a string value? What is it? ); */ } } const domContainer = document.querySelector('#like_button_container'); ReactDOM.render(e(LikeButton), domContainer); // It is the main idea. -->
<script src="https://unpkg.com/react@17/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <div id="like_button_container_jsx"></div> <!-- Load our React component. --> <script type="text/babel" src="like_button_jsx.js"><!-- See the above code for this example. -->
<script src="https://unpkg.com/react@17/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <div id='hello-world-container'></div> <script type="text/babel"> ReactDOM.render( <h1>Hello, world!</h1>, // ? document.getElementById('hello-world-container') ); </script>
$ npx create-react-app my-react-app
$ cd my-react-app
"start": "react-scripts start"
-> "start": "PORT=yourport react-scripts start"
$ npm start
$ npm i web-vitals --save-dev
and start, $ chmod +x node_modules/.bin/react-scripts
and start, and$ npm run build
. See the last subsection..map()
,
Destructuring, and
Modules.
.map()
, arrow function, and template literal.
const data = ['apple', 'banana', 'orange']; const list = data.map((item) => `<li>${item}</li>`); //const list = data.map(function(item) { return `<li>${item}</li>`; }); alert(list);
.map()
!
{ ... }
.className
is used instead of class in a start tag.onEventname
is used instead of oneventname in a start tag. E.g., onClick
const myelement = <h1>I Love JSX {5 + 5} times!</h1>; // It is an example of JSX. One top level HTML element. const myAnoterhExample = ( <> <hr /> <h1 className='test'>I Love JSX {5 + 5} times!</h1> </> ) ReactDOM.render(myelement, document.getElementById('root'));
<ComponentName [attr=value ...] />
<h2>What a great day!</h2>
{ ... }
within JSX code in a componentReactDOM.render(<Weather islike='nice' />, document.getElementById('tr2-root3'));
let data=...; ReactDOM.render(<Weather attrname={data} />, document.getElementById('tr2-root3'));
data={ {name:"John", age:23} }
{data, setData} = useState([0, 1, 2, 3]); ... data[2] = 22; setData(data); // It won't work. ... let tmpData = []; for (let i = 0; i < data.length; i++) tmpData[i] = data[i]; setData(tmpData); // It will work. data = tmpData;
<div id='root'>
import React, { useState } from "react"; import ReactDOM from "react-dom/client"; import "./index.css"; // Check this out! import App from "./App.js"; const root = ReactDOM.createRoot(document.getElementById('root)); root.render(<App />);
body { margin:5px; }
import "./App.css"; import { useState } from "react"; const App = () { // From Trial 6 const [data, setDataaa] = useState("Message here!"); const readTR6 = (message) => { // To be called from the event listener on the next button alert(message); // data = message; // Not this way!!! setDataaa(message); // Will data be bound to the <p> in the below? // How? } return ( <> <input id="tr6-input" /> <button onClick={ () => { readTR6(document.getElementById("tr6-input").value); } }>Read!</button> <p>{ data }</p> {/* maybe another component */} </> ) } export default App;
p { font-size:40px; background-color:Cyan; }
$ npm start
{ "homepage":"https://servername/...", "name": "my-react", "version": "0.1.0", "private": true, ... } // or (This could be better. Let's use this.) { "homepage":".", "name": "my-react", "version": "0.1.0", "private": true, ... }
$ npm run build
$ chmod +x node_modules/.bin/react-scripts
and rebuild.
$ cp -r build/* ~/public_html/react-app
$ find ~/public_html/react-app -type d -exec chmod 711 {} \;
$ find ~/public_html/react-app -type f -exec chmod 644 {} \;